import Vue from 'vue'
import Vuex from 'vuex'
import state from './state'
import mutations from './mutations'

Vue.use(Vuex)

export default new Vuex.Store({
  state,
  mutations
})

/*
let defaultCity = '深圳'
try {
  if (localStorage.city) {
    defaultCity = localStorage.city
  }
} catch (e) {}

export default new Vuex.Store({
  state: {
    city: defaultCity
  },
  // 组件中this.$store.dispatch('changeCity', city)调用了actions，actions通过commit调用mutations，最后mutations改变公用数据
  // actions: {
  //   changeCity (ctx, city) {
  //     console.log(city)
  //     ctx.commit('mutationsChangeCity', city)
  //   }
  // },
  mutations: {
    // state 指的就是所有的共用数据，city就是ctx.commit传过来的数据
    // mutationsChangeCity (state, city) {
    //   state.city = city
    // }
    changeCity (state, city) {
      state.city = city
      // 接收数据同时设置一个本地存储
      try {
        localStorage.city = city
      } catch (e) {}
    }
  }
})
*/
